package liming.tool.maven.info;

import liming.tool.json.ToJson;
import liming.tool.json.annotation.JsonMark;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class Dependency implements ToJson {
    @JsonMark
    private String groupId;
    @JsonMark
    private String artifactId;
    @JsonMark
    private String version;
    private final Map<String, String> properties = new HashMap<>();

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getArtifactId() {
        return artifactId;
    }

    public void setArtifactId(String artifactId) {
        this.artifactId = artifactId;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Map<String, String> getProperties() {
        return properties;
    }

    @Override
    public JSONObject serialize() {
        JSONObject serialize = ToJson.super.serialize();
        properties.forEach(serialize::put);
        return serialize;
    }

    @Override
    public void deserializeBody(JSONObject json) {
        ToJson.super.deserializeBody(json);
        json.keySet().forEach(key -> {
            if (!key.equals("groupId") && !key.equals("artifactId") && !key.equals("version")) {
                this.properties.put(key, json.getString(key));
            }
        });
    }
}
